try:
    import scapy.all as scapy
except ImportError:
    import scapy

try:
    # This import works from the project directory
    import scapy_http.http
except ImportError:
    # If you installed this package via pip, you just need to execute this
    from scapy.layers import http
import os


HOST = []
newUrl = []


def main():
    oldUrl = os.listdir('./host')
    L = os.listdir('./pcapFile')
    for i in L:
        if i[0] != '.':
            if not i + '.txt' in oldUrl:
                newUrl.append(i)
    startAnalysis(newUrl)


def startAnalysis(newUrl):
    for i in newUrl:
        url = ['./pcapFile', i, 'capture.pcap']
        j = '/'.join(url)
        print j
        portUrl = ['./pcapFile',i,'port.txt']
        port = '/'.join(portUrl)
        f= open(port,"r")
        arr = f.read().split(",")
        packets = scapy.rdpcap(j)
        printHost(packets,arr)
        writefile(i, HOST)


def printHost(packets,ports):
    for p in packets:

        sport = "\n".join(p.sprintf("{TCP:%TCP.sport%}\n").split(r"\r\n"))

        try:
            content = str(p)
        except:
            print 'err'
            continue

        j= sport.split("\n")[0]
        if ports[0] == "":
            addHost(content)
        elif j != "":
            if j in ports:
                addHost(content)


def addHost(content):
    if 'HTTP' in content:
        arr = content.split('\n')
        for host in arr:
            if 'Host' in host:
                if not str(host) in HOST:
                    HOST.append(host)

def writefile(filename, stringlist):
    f = open('./host/' + filename + ".txt", "w")
    for i in stringlist:
        f.write(i + "\n")
    f.close()
    print HOST
    del HOST[:]
    # shutil.copy(rootpath+filename,open_url+filename)


if __name__ == '__main__':
    main();
